﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WarehouseManagement.BusinessLogic.DTO;
using WarehouseManagement.BusinessLogic.DTOConverter;
using WarehouseManagement.DataAccess;

namespace WarehouseManagement.BusinessLogic.BC
{
    public class SupplierBC : BaseBC
    {
        public IEnumerable<SupplierDTO> GetSupplierList()
        {
            return Context.Suppliers.Select(e => SupplierConverter.Convert(e)).AsEnumerable();
        }

        public void DeleteSupplier(int supplierId)
        {
            var entity = GetEntity(supplierId);
            if (entity != null)
            {
                Context.Suppliers.DeleteOnSubmit(entity);
                SubmitChanges();
            }
        }

        private Supplier GetEntity(int supplierId)
        {
            return Context.Suppliers.SingleOrDefault(e => e.Id == supplierId);
        }

        public void AddSupplier(SupplierDTO dto)
        {
            var entity = dto.ToEntity();
            Context.Suppliers.InsertOnSubmit(entity);
            SubmitChanges();
        }

        public void UpdateSupplier(SupplierDTO dto)
        {
            var entity = GetEntity(dto.Id);

            entity.SupplierName = dto.SupplierName;
            entity.Address = dto.Address;
            entity.BankAccount = dto.BankAccount;
            entity.BankName = dto.BankName;
            entity.Email = dto.Email;
            entity.MobilePhone = dto.MobilePhone;

            SubmitChanges();
        }
    }
}
